import java.util.ArrayList;
import java.util.List;

public class Test2 {
    //leetcode 全排列
    //https://leetcode.cn/problems/VvJkup/description/
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] vis;
    int count;
    public List<List<Integer>> permute(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        vis = new boolean[nums.length];
        count = nums.length;
        backTracing(nums);
        return ret;
    }

    private void backTracing(int[] nums) {
        if (path.size() == count) {
            ret.add(new ArrayList<>(path));
        }
        for (int i = 0; i < nums.length; i++) {
            if (!vis[i]) {
                vis[i] = true;
                path.add(nums[i]);
                backTracing(nums);
                path.remove(path.size()-1);
                vis[i] = false;
            }
        }
    }
}
